<?php
error_reporting("E_ALL");ini_set("display_errors", 1);
header('Content-Type: text/html; charset=utf-8');
include("include/function.php");
$config = include("config.php");
include("include/".$config['DB_TYPE'].".class.php");
$params=$result='';
if(!empty($_POST['dosubmit'])){
    $params=trim(urldecode(base64_decode($_POST['params'])));
    if(!empty($params)){
        $db = new db;
        $db->connect($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME'], $config['DB_PCONNECT']);

        $list  = explode('</tr>',strip_tags($params,'<tr>,<td>'));
        $msg='';
        $msg.='<a href="getlist.php" target="blank">更新模型列表</a><br>';
        foreach($list as $v){
            $arr1=explode('</td>',$v);
            foreach($arr1 as $key=> $val){$arr1[$key]=trim(strip_tags($val));}
            $arr2=explode('/',$arr1[8]);$arr2=array_map('trim',$arr2);
            if(empty($arr1[1]))continue;
            $item=[];
            $item['host']=strtr($arr1[1],['http://'=>'','https://'=>'']);
            $item['country_name']=$arr2[0];
            $item['address']=implode('/',$arr2);
            $item['port']=$arr1[4];
            $item['adddate']=$arr1[10];
            $item['addtime']=time();
            $item['updatetime']=0;
            $check = $db->get_one("select id from ollama_domain where host='".$item['host']."'");
            if(empty($check)){
                $msg.= $item['host']." 添加成功<br>\n";
                $db->query("insert into ollama_domain(`".implode('`,`',array_keys($item))."`) values('".implode("','",$item)."')");
            }else{
                $msg.= $item['host']." 已存在<br>\n";
            }
        }
        $return = array('status'=>1,'msg'=>$msg);
    }else{
        $return = array('status'=>0,'msg'=>'提交数据为空');
    }
    ajaxReturn($return);
}
?>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title>ollama格式化工具</title></head>
<script src="jquery.min.js"></script>
<body>
<textarea style="width:100%; height:300px" name="params" id="params"></textarea>
<br />
<input type="button" name="dosubmit" value="SUBMIT" onclick="dosubmit()"/>
<br />
<div id="msg"></div>
<script>
function dosubmit(){
    var url = "index.php";
    var htmlString = $('#params').val();
    if(!htmlString || htmlString==''){alert('请填写内容');return;}
    var content = htmlString.replace(/<img[^>]*>/ig, '');
    var query = new Object();
	query.params =utf8ToB64(content);
    query.dosubmit=1;
	$.ajax({
		url: url,
		data: query,
		type: "post",
		dataType: "json",
		success: function (res) {
			if(res.status==1){
			    $('#msg').html(res.msg);
			}else{
			     alert(res.msg);
			}
		}
	});
}
function utf8ToB64(str) {
  return btoa(unescape(encodeURIComponent(str)));
}
</script>
</body></html>